<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="commonServerCode">
              <el-input v-model="filters.commonServerCode" placeholder="货品编号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="name">
              <el-input v-model="filters.name" placeholder="货品名称" clearable></el-input>
            </el-form-item>
            <el-form-item prop="classifyIds" v-if="userRole == 1">
              <el-cascader clearable filterable v-model="filters.classifyIds" :options="treeThreeServerList" :props="{ checkStrictly: true, label: 'classifyName', value: 'serverClassifyId' }" placeholder="货品分类">
              </el-cascader>
            </el-form-item>
            <el-form-item prop="variantState">
              <el-select v-model="filters.variantState" clearable filterable placeholder="规格">
                <el-option :value="1" label="单品"> </el-option>
                <el-option :value="2" label="多规格"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="flag">
              <!-- <el-checkbox v-model="filters.visitFlag">上门</el-checkbox> -->
              <el-radio-group v-model="filters.flag">
                <el-radio :label="1"> 上门 </el-radio>
                <el-radio :label="2"> 到店 </el-radio>
                <el-radio :label="3"> 远程 </el-radio>
              </el-radio-group>
            </el-form-item>
            <!-- <el-form-item prop="shopFlag">
              <el-checkbox v-model="filters.shopFlag">到店</el-checkbox>
            </el-form-item>
            <el-form-item prop="remoteFlag">
              <el-checkbox v-model="filters.remoteFlag">远程</el-checkbox>
            </el-form-item> -->
            <el-form-item prop="fastFlag">
              <el-checkbox v-model="filters.fastFlag">紧急通知</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增 </el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table
        ref="tableList"
        :data="dataList"
        :fit="true"
        highlight-current-row
        v-loading="listLoading"
        @selection-change="handleSelectionChange"
        element-loading-text="数据加载中，请稍后..."
        empty-text="对不起，没有找到相关数据！"
      >
        <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="货品编号" prop="commonServerCode"> </el-table-column>
        <el-table-column align="center" label="货品图片" width="160">
          <template slot-scope="scope">
            <!-- <div class="img-wrapper"> -->
            <el-image title="点击可预览" :src="scope.row.mainImage" fit="cover" :preview-src-list="[scope.row.mainImage]" :z-index="scope.$index"> </el-image>
            <!-- </div> -->
          </template>
        </el-table-column>
        <el-table-column align="center" label="货品名称" prop="name"> </el-table-column>
        <el-table-column align="center" label="货品分类">
          <template slot-scope="scope"> {{ scope.row.serverClassifyName }}-{{ scope.row.thirdClassifyName }} </template>
        </el-table-column>
        <el-table-column align="center" label="规格">
          <template slot-scope="scope">
            <el-tag effect="dark" size="mini" v-if="scope.row.variantState == 1"> 单品 </el-tag>
            <el-tag v-else type="success" size="mini" effect="dark"> 多规格 </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="上门/到店/远程/紧急通知" width="120">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" effect="dark" v-if="scope.row.visitFlag == 1"> 上门 </el-tag>
              <el-tag size="mini" effect="dark" v-if="scope.row.shopFlag == 1" type="success"> 到店 </el-tag>
              <div></div>
              <el-tag size="mini" effect="dark" v-if="scope.row.remoteFlag == 1" type="warning"> 远程 </el-tag>
              <el-tag size="mini" effect="dark" v-if="scope.row.fastFlag == 1" type="danger"> 急 </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否需配送费" width="120">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" effect="dark" v-if="scope.row.deliveryFlag == 0" type="info"> 否 </el-tag>
              <el-tag size="mini" effect="dark" v-if="scope.row.deliveryFlag == 1"> 是 </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="原价" prop="price"></el-table-column>
        <el-table-column align="center" label="售价" prop="salePrice"></el-table-column>
        <el-table-column align="center" label="包装费" prop="packageFee"></el-table-column>
        <!--<el-table-column align="center" label="配送费" prop="deliveryFee"></el-table-column>-->
        <el-table-column align="center" width="90" label="创建时间" prop="createDate"></el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑 </el-dropdown-item>
                <!-- <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{scope.row.state === 0 ? '启用' : '禁用'
                  }}</el-dropdown-item> -->
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import CommonServerApi from '@/api/goods/commonServer';
import BusinessApi from '@/api/business/business';
import ServerClassifyApi from '@/api/server/serverClassify';
import batchDistribution from '@/components/batchDistribution';
import * as message from '@/constants/message';
import { getStorage } from '@/utils/auth';
import { USER_INFO } from '@/constants/storeName';

export default {
  name: 'CommonServerManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      userRole: '',
      dataList: [],
      listLoading: true,
      imgList: [], //货品图片
      filters: {
        commonServerCode: '',
        name: '',
        classifyIds: [],
        variantState: '',
        // visitFlag: false,
        // shopFlag: false,
        // remoteFlag: false,
        flag: '',
        fastFlag: false,
      },
      selectedForm: {
        ids: [],
        state: '',
      },
      treeThreeServerList: [],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionAdd: 'SYSTEM_MANAGER_GOODS_COMMONSERVER_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_GOODS_COMMONSERVER_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_GOODS_COMMONSERVER_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_GOODS_COMMONSERVER_LIST_DELETE',
    };
  },
  components: {
    batchDistribution,
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      // 获取用户的角色信息
      _this.userRole = getStorage(USER_INFO).userRole;
      _this.listLoading = true;
      _this.getTreeThreeServerList();
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSelectionChange(value) {
      this.selectedForm.ids = value.map((item) => item.commonServerId);
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/goods/commonServerAdd');
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/goods/commonServerEdit',
        query: { id: row.commonServerId },
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/goods/commonServerDetail',
        query: { id: row.commonServerId },
      });
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state === 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        CommonServerApi.deletecommonServer({
          id: row.commonServerId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        CommonServerApi.deleteCommonServer({
          id: val.commonServerId,
          state: -1,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    // 获取二三级服务列表
    getTreeThreeServerList() {
      ServerClassifyApi.listServerClassify({ level: 2 }).then((res) => {
        this.treeThreeServerList = res.data;
      });
    },
    listData() {
      // 获取列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
      };
      cond.name = _this.filters.name;
      cond.serverClassifyId = _this.filters.classifyIds[0] ? _this.filters.classifyIds[0] : '';
      cond.thirdClassifyId = _this.filters.classifyIds[1] ? _this.filters.classifyIds[1] : '';
      // cond.visitFlag = _this.filters.visitFlag ? 1 : ''
      // cond.shopFlag = _this.filters.shopFlag ? 1 : ''
      // cond.remoteFlag = _this.filters.remoteFlag ? 1 : ''
      cond.fastFlag = _this.filters.fastFlag ? 1 : '';

      cond.commonServerCode = _this.filters.commonServerCode;
      cond.variantState = _this.filters.variantState;

      if (_this.filters.flag == 1) cond.visitFlag = 1;
      if (_this.filters.flag == 2) cond.shopFlag = 1;
      if (_this.filters.flag == 3) cond.remoteFlag = 1;

      CommonServerApi.listCommonServer(cond)
        .then((res) => {
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
          _this.imgList = res.data.list.map((item) => item.mainImage);
          _this.listLoading = false;
          _this.$refs.tableList.bodyWrapper.scrollTop = 0;
          setTimeout(() => {
            _this.$refs.tableList.doLayout();
          });
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.img-wrapper {
  width: 80px;
  height: 60px;
  overflow: hidden;
  border-radius: 3px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 0 auto;

  .el-image {
    width: 100%;

    &::v-deep img {
      width: 100%;
      height: auto;
    }
  }
}
</style>
